<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>7.C#正则表达式 - Coding Night - A personal blog for Rango</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Rango" /><meta name="description" content="C# 正则表达式 正则表达式是一种匹配文本的模式。.net框架提供了允许这种匹配的正则表达式引擎。模式由一个或多个字符、运算符和结构组成。 定义正则" />






<meta name="generator" content="Hugo 0.97.3 with theme even" />


<link rel="canonical" href="https://rangolee.github.io/post/cshape/07_c#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.a659bb31b17a54f7792e1bc783fc75b31118e6a175b42337b03fae0b1a3ef2b5.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="7.C#正则表达式" />
<meta property="og:description" content="C# 正则表达式 正则表达式是一种匹配文本的模式。.net框架提供了允许这种匹配的正则表达式引擎。模式由一个或多个字符、运算符和结构组成。 定义正则" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://rangolee.github.io/post/cshape/07_c#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2016-05-28T00:00:00+00:00" />
<meta property="article:modified_time" content="2016-05-28T00:00:00+00:00" />

<meta itemprop="name" content="7.C#正则表达式">
<meta itemprop="description" content="C# 正则表达式 正则表达式是一种匹配文本的模式。.net框架提供了允许这种匹配的正则表达式引擎。模式由一个或多个字符、运算符和结构组成。 定义正则"><meta itemprop="datePublished" content="2016-05-28T00:00:00+00:00" />
<meta itemprop="dateModified" content="2016-05-28T00:00:00+00:00" />
<meta itemprop="wordCount" content="534">
<meta itemprop="keywords" content="CShape," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="7.C#正则表达式"/>
<meta name="twitter:description" content="C# 正则表达式 正则表达式是一种匹配文本的模式。.net框架提供了允许这种匹配的正则表达式引擎。模式由一个或多个字符、运算符和结构组成。 定义正则"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Coding Night</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archives</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Categories</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">About</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Coding Night</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archives</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Categories</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">About</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">7.C#正则表达式</h1>

      <div class="post-meta">
        <span class="post-time"> 2016-05-28 </span>
        <div class="post-category">
            <a href="/categories/cshape/"> CShape </a>
            </div>
          <span class="more-meta"> 约 534 字 </span>
          <span class="more-meta"> 预计阅读 2 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#c-正则表达式">C# 正则表达式</a>
          <ul>
            <li><a href="#定义正则表达式">定义正则表达式</a></li>
            <li><a href="#字符类">字符类</a></li>
            <li><a href="#定位点">定位点</a></li>
            <li><a href="#分组构造">分组构造</a></li>
            <li><a href="#限定符">限定符</a></li>
            <li><a href="#反向引用构造">反向引用构造</a></li>
            <li><a href="#备用构造">备用构造</a></li>
            <li><a href="#替换">替换</a></li>
            <li><a href="#杂项构造">杂项构造</a></li>
            <li><a href="#regex类">Regex类</a></li>
          </ul>
        </li>
        <li><a href="#实例1">实例1</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="c-正则表达式">C# 正则表达式</h2>
<hr>
<p><strong>正则表达式</strong>是一种匹配文本的模式。.net框架提供了允许这种匹配的正则表达式引擎。模式由一个或多个字符、运算符和结构组成。</p>
<hr>
<h3 id="定义正则表达式">定义正则表达式</h3>
<p>下面列出了用于定义正表达式的各种类别的字符、运算符和结构。</p>
<ul>
<li>字符转义</li>
</ul>
<ul>
<li>字符类</li>
</ul>
<ul>
<li>定位点</li>
<li>分组结构</li>
<li>限定符</li>
<li>反向引用构造</li>
<li>备用构造</li>
<li>替换</li>
<li>杂项构造</li>
</ul>
<!-- more -->
<h4 id="字符转义">字符转义</h4>
<p>正则表达式中的反斜杠符号（\）指示其后跟的字符是特殊字符，或应按原义解释该字符。
<img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-28%2022.17.53.png" alt=""></p>
<h3 id="字符类">字符类</h3>
<p>字符类与一组字符中的任何一个字符匹配
<img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-29%2023.12.18.png" alt=""></p>
<h3 id="定位点">定位点</h3>
<p>定位点或原子零宽度断言会使匹配成功或失败，具体取决于字符串中的当前位置，但它们不会使引擎在字符串中前进或使用字符。
<img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-29%2023.20.35.png" alt=""></p>
<h3 id="分组构造">分组构造</h3>
<p>分组构造描述了正则表达式的子表达式，通常用于捕获输入字符串的子字符串。
<img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-29%2023.26.23.png" alt=""></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-c#" data-lang="c#"><span class="line"><span class="cl">    <span class="kt">string</span> <span class="n">input</span> <span class="p">=</span> <span class="s">&#34;1851 1999 1950 1905 2003&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">string</span> <span class="n">pattern</span> <span class="p">=</span> <span class="s">@&#34;(?&lt;=19)\d{2}\b&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">foreach</span> <span class="p">(</span><span class="n">Match</span> <span class="n">match</span> <span class="k">in</span> <span class="n">Regex</span><span class="p">.</span><span class="n">Matches</span><span class="p">(</span><span class="n">input</span><span class="p">,</span> <span class="n">pattern</span><span class="p">))</span>
</span></span><span class="line"><span class="cl">        <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">match</span><span class="p">.</span><span class="n">Value</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="限定符">限定符</h3>
<p>限定符指定在输入字符串中必须存在上一个元素（可以是字符。组或字符类）的多少个实例才能出现匹配项。限定符包括下表中列出的语言元素。</p>
<p><img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-30%2022.44.13.png" alt=""></p>
<h3 id="反向引用构造">反向引用构造</h3>
<p>反向引用构造允许在同一正则表达式中随后标识以前匹配的自表达式。
<img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-30%2022.47.44.png" alt=""></p>
<h3 id="备用构造">备用构造</h3>
<p>备用构造用于修正正则表达式以启用either/or匹配
<img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-30%2022.49.53.png" alt=""></p>
<h3 id="替换">替换</h3>
<p>替换是替换模式中使用的正则表达式
<img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-30%2022.52.59.png" alt=""></p>
<h3 id="杂项构造">杂项构造</h3>
<p><img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-30%2022.54.20.png" alt=""></p>
<h3 id="regex类">Regex类</h3>
<p><strong>Regex</strong>类用于表示一个正则表达式
<img src="C%23%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-12-30%2022.55.16.png" alt="">
如需了解Regex类的完整属性列表，请参阅微软的C#文档</p>
<hr>
<h2 id="实例1">实例1</h2>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Rango</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2016-05-28
        
    </span>
  </p>
  
  
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/wechat.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/alipay.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/cshape/">CShape</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/others/git&#43;hexoblog/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">如何使用hexo搭建git博客</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/cshape/06_c#%E5%B8%B8%E9%87%8F/">
            <span class="next-text nav-default">6.C#常量</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:rangodev@163.com" class="iconfont icon-email" title="email"></a>
  <a href="https://rangolee.github.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2016 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span>Rango</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.64437849d125a2d603b3e71d6de5225d641a32d17168a58106e0b61852079683.js"></script>








</body>
</html>
